package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.ProductDelistings;
import com.example.mall.dto.ProductDelistingsDto;
import com.example.mall.mapper.ProductDelistingsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.ProductDelistingsQuery;
import com.example.mall.service.IProductDelistingsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商品下架记录表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-06
 */
@Service
public class ProductDelistingsServiceImpl extends ServiceImpl<ProductDelistingsMapper, ProductDelistings> implements IProductDelistingsService {

    @Autowired
    private ProductDelistingsMapper productDelistingsMapper;

    @Override
    public PageDTO<ProductDelistingsDto> queryPage(ProductDelistingsQuery productDelistingsQuery) {
        if (productDelistingsQuery == null) {
            productDelistingsQuery = new ProductDelistingsQuery();
        }
        Page<ProductDelistings> page = productDelistingsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<ProductDelistings> cartPage = lambdaQuery()
                .like(productDelistingsQuery.getDelistId()!= null, ProductDelistings::getDelistId, productDelistingsQuery.getDelistId())
                .like(productDelistingsQuery.getProductId()!= null, ProductDelistings::getProductId, productDelistingsQuery.getProductId())
                .like(productDelistingsQuery.getDelistReas()!= null, ProductDelistings::getDelistReas, productDelistingsQuery.getDelistReas())
                .apply(productDelistingsQuery.getDelistTime() != null,
                        "DATE_FORMAT(delist_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        productDelistingsQuery.getDelistTime())
                .orderByDesc(ProductDelistings::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, ProductDelistingsDto.class);
    }

    @Override
    public ProductDelistings queryProductDelistingsById(Long delistId) {
        AssertUtil.isNotNull(delistId, "delistId不能为空");
        return productDelistingsMapper.selectById(delistId);
    }

    @Override
    public List<ProductDelistings> queryProductDelistingsList() {
        return this.list();
    }

    @Override
    public void insert(ProductDelistingsDto productDelistingsDto) {
        ProductDelistings productDelistings = new ProductDelistings();
        CopyUtils.copyProperties(productDelistingsDto, productDelistings);
        productDelistings.setDelistId(CodeGenerateUtils.generateUniqueId());
        productDelistings.setCreateDate(new Date());
        productDelistings.setUpdateDate(new Date());
        productDelistingsMapper.insert(productDelistings);
    }

    @Override
    public void update(ProductDelistingsDto productDelistingsDto) {
        AssertUtil.isNotNull(productDelistingsDto.getDelistId(), "delistId不能为空");
        ProductDelistings productDelistings = productDelistingsMapper.selectById(productDelistingsDto.getDelistId());
        AssertUtil.isNotNull(productDelistings, "商品下架记录不存在");
        CopyUtils.copyProperties(productDelistingsDto, productDelistings);
        productDelistings.setUpdateDate(new Date());
        productDelistingsMapper.updateById(productDelistings);
    }

    @Override
    public void delete(Long delistId) {
        AssertUtil.isNotNull(delistId, "delistId不能为空");
        ProductDelistings productDelistings = productDelistingsMapper.selectById(delistId);
        AssertUtil.isNotNull(productDelistings, "商品下架记录不存在");
        productDelistingsMapper.deleteById(delistId);
    }

    @Override
    public void deleteBatch(List<Long> delistIds) {
        AssertUtil.isNotNull(delistIds, "delistIds不能为空");
        List<ProductDelistings> productDelistings = productDelistingsMapper.selectBatchIds(delistIds);
        AssertUtil.isNotNull(productDelistings, "商品下架记录不存在");
        productDelistingsMapper.deleteBatchIds(delistIds);
    }
}
